<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form *ngIf="!isLoading" [formGroup]="userForm" (ngSubmit)="onSubmit()" gioFormFocusInvalid>
  <h1><gio-go-back-button routerLink=".."></gio-go-back-button>Pre-register a user</h1>
  <h4>The new user will receive an email to finalize the creation by setting a password.</h4>

  <mat-card class="org-settings-new-user__card">
    <mat-card-content>
      <h2>User type</h2>

      <gio-form-selection-inline class="org-settings-new-user__card__provider-type-group" formControlName="type">
        <gio-form-selection-inline-card class="org-settings-new-user__card__user-type-group__button" value="EXTERNAL_USER">
          <mat-icon class="org-settings-new-user__card__user-type-group__button__logo" matPrefix>perm_identity</mat-icon>
          <p class="org-settings-new-user__card__user-type-group__button__label">User</p>
        </gio-form-selection-inline-card>

        <gio-form-selection-inline-card class="org-settings-new-user__card__user-type-group__button" value="SERVICE_ACCOUNT">
          <mat-icon class="org-settings-new-user__card__user-type-group__button__logo" matPrefix>manage_accounts</mat-icon>
          <p class="org-settings-new-user__card__user-type-group__button__label">Service Account</p>
        </gio-form-selection-inline-card>
      </gio-form-selection-inline>
    </mat-card-content>
  </mat-card>

  <mat-card class="org-settings-new-user__card">
    <mat-card-content *ngIf="userForm.get('type').value === UserType.EXTERNAL_USER">
      <h2>User</h2>
      <mat-form-field class="org-settings-new-user__card__form-field">
        <mat-label>First Name</mat-label>
        <input matInput formControlName="firstName" required data-testid="firstName" />
        <mat-error *ngIf="userForm.get('firstName').errors?.required">This field is required.</mat-error>
      </mat-form-field>

      <mat-form-field class="org-settings-new-user__card__form-field">
        <mat-label>Last Name</mat-label>
        <input matInput formControlName="lastName" required data-testid="lastName" />
        <mat-error *ngIf="userForm.get('lastName').errors?.required">This field is required.</mat-error>
      </mat-form-field>

      <mat-form-field class="org-settings-new-user__card__form-field">
        <mat-label>Email</mat-label>
        <input matInput formControlName="email" type="email" required data-testid="email" />
        <mat-error *ngIf="userForm.get('email').errors?.required">This field is required.</mat-error>
        <mat-error *ngIf="userForm.get('email').errors?.email">This field should be a valid email.</mat-error>
      </mat-form-field>

      <ng-container *ngIf="identityProviders && identityProviders.length > 1">
        <h2>Identity Provider</h2>

        <mat-form-field class="org-settings-new-user__card__form-field">
          <mat-label>Name</mat-label>
          <mat-select formControlName="source" required>
            <mat-option *ngFor="let idp of identityProviders" [value]="idp.id"> {{ idp.name }}</mat-option>
          </mat-select>

          <mat-error *ngIf="userForm.get('email').errors?.required">This field is required.</mat-error>
          <mat-error *ngIf="userForm.get('email').errors?.email">This field should be a valid email.</mat-error>
        </mat-form-field>

        <mat-form-field *ngIf="userForm.get('source').value !== 'gravitee'" class="org-settings-new-user__card__form-field">
          <mat-label>Identifier</mat-label>
          <input matInput formControlName="sourceId" required />
          <mat-error *ngIf="userForm.get('sourceId').errors?.required">This field is required.</mat-error>
        </mat-form-field>
      </ng-container>
    </mat-card-content>

    <mat-card-content *ngIf="userForm.get('type').value === UserType.SERVICE_ACCOUNT">
      <h2>Service Account</h2>

      <mat-form-field class="org-settings-new-user__card__form-field">
        <mat-label>Service Name</mat-label>
        <input matInput formControlName="lastName" required data-testid="lastName" />
        <mat-hint>Choose a meaningful name for your service account.</mat-hint>
        <mat-error *ngIf="userForm.get('lastName').errors?.required">This field is required.</mat-error>
      </mat-form-field>

      <mat-form-field class="org-settings-new-user__card__form-field">
        <mat-label>Email</mat-label>
        <input matInput formControlName="email" type="email" data-testid="email" />
        <mat-error *ngIf="userForm.get('email').errors?.email">This field should be a valid email.</mat-error>
      </mat-form-field>
    </mat-card-content>
  </mat-card>

  <gio-save-bar [creationMode]="true" [form]="userForm"> </gio-save-bar>
</form>
